#include "../include/PDTextExtractor.h"
#include <stdio.h>
#include <iostream>
using namespace std;
CPDPageLineInfo::CPDPageLineInfo(IPDFLR_Cell* pLine)
{
	m_pLine = pLine;
}
CPDPageLineInfo::~CPDPageLineInfo()
{
}

CPDF_Rect CPDPageLineInfo::GetLineRect()
{
	CPDF_Rect rc;
	m_pLine->GetBBox(rc);
	return rc;
}

FX_BOOL CPDPageLineInfo::GetLineFontInfo(FX_FLOAT& fontsize, FX_DWORD& fontcolor, FX_BOOL& isCID)
{
	CFX_PtrArray arrFont;
//	CFX_Int32Array arrChars;
	CFX_PtrArray arrTextObjs;
	FX_BOOL bSamePDFont = TRUE;
	FX_BOOL bSameFontFace = TRUE;
	CPDF_Font* pPreFont = NULL;
	CFX_ByteString bsPreFace;

	int iMostCharObj = 0;
	int nPreChars = 0;

	FX_POSITION pos = m_pLine->GetChildCellHeadPos();
	while(pos)
	{
		IPDFLR_Cell* pNext = m_pLine->GetNextChildCell(pos);
		if(NULL == pNext) continue;

		CPDF_PageObject* pObj = pNext->GetPageObject();
		if(NULL == pObj) continue;

		if(pObj->m_Type == PDFPAGE_TEXT)
		{

			CPDF_TextObject* pText = (CPDF_TextObject*)pObj;
			CPDF_Font* pFont = pText->m_TextState.GetFont();
			if(pFont == NULL) continue;

			//is all textobject used same one font
			if(pPreFont == NULL)
				pPreFont = pFont;
			else
			{
				if(bSamePDFont)
				{
					bSamePDFont = pPreFont == pFont;
				}
			}

			//is all text used same face
			if(bsPreFace == "")
				bsPreFace = pFont->m_Font.GetFaceName();
			else
			{
				if(bSameFontFace)
				{
					bSameFontFace = bsPreFace.EqualNoCase(pFont->m_Font.GetFaceName());
				}
			}

			arrFont.Add(pFont);
			arrTextObjs.Add(pText);

			if(nPreChars < pText->CountChars())
			{
				nPreChars = pText->CountChars();
				iMostCharObj = arrTextObjs.GetSize()-1;
			}
	
		}
	}

	if(bSamePDFont || bSameFontFace)
		iMostCharObj = 0;
	
	CPDF_TextObject* pText = (CPDF_TextObject*)arrTextObjs.GetAt(iMostCharObj);

	CFX_AffineMatrix mt;
	pText->GetTextMatrix(&mt);
	fontsize = pText->m_TextState.GetFontSize() * mt.a;
	if(0 == fontsize) fontsize = 10;
	int R = 0;
	int G=0;
	int B=0;
	pText->m_ColorState.GetFillColor()->GetRGB(R, G, B);
	fontcolor = FXSYS_RGB(R,G,B);
	isCID = pText->m_TextState.GetFont()->GetCIDFont() !=  NULL;

	return TRUE;


}
FX_BOOL CPDPageLineInfo::IsIndependLine(FX_FLOAT page_width, FX_FLOAT page_height)
{

	CPDF_Rect rc;
	m_pLine->GetBBox(rc);
	FX_FLOAT w = rc.Width();
	FX_FLOAT h = rc.Height();

	FX_BOOL bIsAllText = TRUE;
	FX_POSITION pos = m_pLine->GetChildCellHeadPos();
	while(pos)
	{
		IPDFLR_Cell* pNext = m_pLine->GetNextChildCell(pos);
		if(NULL == pNext) continue;

		CPDF_PageObject* pObj = pNext->GetPageObject();

		if(pObj->m_Type != PDFPAGE_TEXT)
		{
			bIsAllText = FALSE;
			break;
		}
	}

	if(page_width > w*4 && bIsAllText)
		return TRUE;

	return FALSE;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//class CPDPageParagraphInfo
CPDPageParagraphInfo::CPDPageParagraphInfo(IPDFLR_Cell* pParagraph)
{
	m_pParagraph = pParagraph;
}
CPDPageParagraphInfo::~CPDPageParagraphInfo()
{
	int nLineCount = m_arrLineContent.GetSize();
	
		for(int i=0; i<nLineCount; i++)
		{
			CFX_WideString* pwszContent = (CFX_WideString*)m_arrLineContent.GetAt(i);
			if(NULL == pwszContent) continue;
			delete pwszContent;

			CPDF_Rect* pRect = (CPDF_Rect*)m_arrLineRect.GetAt(i);
			delete pRect;
		}
		m_arrLineContent.RemoveAll();
		m_arrLineRect.RemoveAll();
		m_arrLineCell.RemoveAll();
	
}

void CPDPageParagraphInfo::GetLineIndependInfo(FX_FLOAT page_width, FX_FLOAT page_height, FX_INT32& nLineCount, FX_BOOL& bAllLineIndepend)
{
	FX_POSITION pos = m_pParagraph->GetChildCellHeadPos();
	if(!pos){bAllLineIndepend = FALSE; nLineCount=0; return;}
	bAllLineIndepend = TRUE;
	while(pos)
	{
		IPDFLR_Cell* pNext = m_pParagraph->GetNextChildCell(pos);
		if(NULL == pNext) continue;
		FPDFLR_AttributeType eType = pNext->GetCellType();
		if(eType == FPDFLR_AT_Baseline)
		{
			CPDPageLineInfo info(pNext);
			if(!info.IsIndependLine(page_width, page_height))
			{
				bAllLineIndepend = FALSE;
				break;
			}
			CFX_WideString* pwszContent = FX_NEW CFX_WideString;
			FX_INT32 nLevel2 = 0;
			FX_POSITION pos2 = pNext->GetChildCellHeadPos();
			while(pos2){
				IPDFLR_Cell* pNext2 = pNext->GetNextChildCell(pos2);
				if(pNext2)
				{
					CPDF_PageObject* pObj = pNext2->GetPageObject();
		
					if(pObj && pObj->m_Type == PDFPAGE_TEXT)
					{
						CPDTextExtractor et;
						et.GetObjText(pObj, *pwszContent);
					}
					//	*pwszContent += gGetObjText(pObj);
					if(++nLevel2 > 200) break;
				}
			}

			//*pwszContent = gExtractParagraphContent(pNext, page_width);
			m_arrLineContent.Add(pwszContent);

			CPDF_Rect* pRect = FX_NEW CPDF_Rect;
			pNext->GetBBox(*pRect);
			m_arrLineRect.Add(pRect);

			m_arrLineCell.Add(pNext);

		}
	}
	nLineCount = m_arrLineContent.GetSize();
	if(!bAllLineIndepend)
	{
		for(int i=0; i<nLineCount; i++)
		{
			CFX_WideString* pwszContent = (CFX_WideString*)m_arrLineContent.GetAt(i);
			if(NULL == pwszContent) continue;
			delete pwszContent;
			
			CPDF_Rect* pRect = (CPDF_Rect*)m_arrLineRect.GetAt(i);
			delete pRect;
		}
		m_arrLineContent.RemoveAll();
		m_arrLineRect.RemoveAll();
		m_arrLineCell.RemoveAll();
	}
	
}

